package ss;
import robocode.*;
import robocode.Robot;

import java.util.Random;
import java.awt.*;

/**
 * Psycho - a robot by Sundeep Shakya
 */
public class Psycho extends Robot
{
    Random generator = new Random( 360 );
    /**
	 * run: Psycho's default behavior
	 */
	public void run() {

        setColors(Color.MAGENTA,Color.MAGENTA,Color.MAGENTA,Color.MAGENTA,Color.MAGENTA);
        while (true) {
            move();
		}
    }

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
        this.stop();
        if(e.getDistance() < 10)
            fire(20);
        else if(e.getDistance() < 50)
            fire(5);
        else if(e.getDistance() < 100)
            fire(2);
        else
            fire(1);
        turn();
    }

    private void move(){
        if(generator.nextInt(360) % 2 == 0){

            ahead(Math.max(getBattleFieldHeight(), getBattleFieldWidth())/4);
        }else{
            back(Math.max(getBattleFieldHeight(), getBattleFieldWidth())/4);
        }
        turn();
    }

    public void onHitWall(robocode.HitWallEvent event) {
        turnLeft(generator.nextInt(180));
        ahead(100);
    }

    public void onHitRobot(robocode.HitRobotEvent event) {
        if(event.isMyFault()){
            turnRight(event.getBearing());
            fire(2);
        }
        move();
    }

    public void onHitByBullet(robocode.HitByBulletEvent event) {
        turnLeft(event.getBearing());
        fire(1);
    }

    private void turn(){
        if(generator.nextInt(360) % 2 == 0){
            turnRight(generator.nextInt(360));
        }else{
            turnLeft(generator.nextInt(360));
        }
    }
}
